package com.jihox.abookpro;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jihox.abookpro.R;
import com.jihox.abookpro.util.MyTextWatcher;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 接收手机号，传递给VerifyActivity
 * @author york.gui
 *
 */
public class SignUpActivity extends BaseActivity {
	
	private static final int REGIST_CODE_NETX = 1234322;
    private EditText signPhoneNumber;
    private Button resetPhoneNumber;
    private Button sendAuthCode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_signup);        
        initView();
        initHeader();
        
        //是否显示清空按钮
        signPhoneNumber.addTextChangedListener(new MyTextWatcher(resetPhoneNumber));
        
        //清空手机号
        resetPhoneNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signPhoneNumber.setText(null);
            }
        });

        /**
         * 点击注册按钮
         * 验证手机号码格式
         * 正确则将手机号提交给服务器，跳转至验证注册页面
         * 不正确则Toast提交手机号错误
         */
        sendAuthCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String customerPhoneNumber = signPhoneNumber.getText().toString().trim();               
                Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
                Matcher m = p.matcher(customerPhoneNumber);            
                if ( m.matches()) { 
                	//跳转到VerifyActivity，传递手机号
                    Intent intent = new Intent(SignUpActivity.this, VerifyPassWordActivity.class);
                    intent.putExtra("signUpPhoneNumber", customerPhoneNumber);  
                    //跳转来源信息。来自SignUp，接下来进行确认注册
                    intent.putExtra("From", "SignUp");
                    startActivityForResult(intent, REGIST_CODE_NETX);
                } else {
                    Toast.makeText(SignUpActivity.this, R.string.T_Phone_Format_Error, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    
    private void initView() {        
        signPhoneNumber = (EditText) findViewById(R.id.edit_signup_phone_number);
        resetPhoneNumber = (Button) findViewById(R.id.btn_reset_phone_number);
        sendAuthCode = (Button) findViewById(R.id.btn_send_authcode);
    }
    /**
     * 初始化头部
     */
    private void initHeader(){
    	//头部半透明
    	RelativeLayout rl_head = (RelativeLayout) this.findViewById(R.id.signup);
    	rl_head.setBackgroundColor(Color.argb(60, 0, 0, 0));
    	//注册返回按钮事件
		Button backBtn = (Button) findViewById(R.id.ivBack);
		backBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {				
				SignUpActivity.this.finish();
			}
		});
		//设置Title文字为注册
		TextView center = (TextView) findViewById(R.id.tvTitle);
		center.setText(R.string.Sign_Up);  
		
		//设置登录按钮  点击跳转到登录活动页面
//		Button btnLogin = (Button) findViewById(R.id.head_btnOperation);
//		btnLogin.setText(R.string.X_Login);
//		btnLogin.setVisibility(View.VISIBLE);
//		btnLogin.setOnClickListener(new OnClickListener(){
//
//			@Override
//            public void onClick(View view) {
//                Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
//                startActivity(intent); 
//                finish();
//            }
//        });		
	}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	switch (requestCode) { 
 	   	case REGIST_CODE_NETX:
 	   		if(resultCode == RESULT_OK){
 	   			setResult(RESULT_OK);
 	   			finish();
 	   		}
 	    break;
    	}
    	super.onActivityResult(requestCode, resultCode, data);
    }
}

